package com.yanqu.road.dao.impl.curio;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.curio.CurioLotteryNoticeData;
import com.yanqu.road.utils.date.DateHelper;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CurioLotteryNoticeDataDaoImpl extends TempDataObjectDao<CurioLotteryNoticeData> {

    public static String getCurioLotteryNoticeDataSql = "select * from `t_c_curio_lottery_notice` where `time` > ?";
    public Map<Long, List<CurioLotteryNoticeData>> getCurioLotteryNoticeData() {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Long, List<CurioLotteryNoticeData>> map = new HashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall(getCurioLotteryNoticeDataSql);
            int i = 1;
            long time = System.currentTimeMillis() - DateHelper.DAY_MILLIONS * 7;
            call.setLong(i++, time);
            rs = call.executeQuery();
            while (rs.next()) {
                CurioLotteryNoticeData data = parse(rs);
                if(!map.containsKey(data.getServerId())){
                    map.put(data.getServerId(), new ArrayList<>());
                }
                map.get(data.getServerId()).add(data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }

    public static String updateCurioLotteryNoticeDataSql = "update `t_c_curio_lottery_notice` set userId=?,type=?,rewardId=?,time=? where serverId=?";
    @Override
    protected boolean updateData(CurioLotteryNoticeData curioLotteryNoticeData) {
//        curioLotteryNoticeData.setBeginWriteTime();
//        getDb().setConnName(DbNameStrategy.getCross2Db());
//        int i = 1;
//        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
//        para.put(i++, new DbParameter(Types.BIGINT, curioLotteryNoticeData.getUserId()));
//        para.put(i++, new DbParameter(Types.INTEGER, curioLotteryNoticeData.getType()));
//        para.put(i++, new DbParameter(Types.VARCHAR, curioLotteryNoticeData.getRewardId()));
//        para.put(i++, new DbParameter(Types.BIGINT, curioLotteryNoticeData.getTime()));
//        para.put(i++, new DbParameter(Types.BIGINT, curioLotteryNoticeData.getServerId()));
//        if (getDb().executeNoneQuery(updateCurioLotteryNoticeDataSql, para) > 0) {
//            return true;
//        }
//        return false;
        return true;
    }

    public static String insertCurioLotteryNoticeDataSql = "insert into `t_c_curio_lottery_notice`(serverId,userId,type,rewardId,time) values (?,?,?,?,?)";
    @Override
    protected boolean addData(CurioLotteryNoticeData curioLotteryNoticeData) {
        curioLotteryNoticeData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, curioLotteryNoticeData.getServerId()));
        para.put(i++, new DbParameter(Types.BIGINT, curioLotteryNoticeData.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, curioLotteryNoticeData.getType()));
        para.put(i++, new DbParameter(Types.VARCHAR, curioLotteryNoticeData.getRewardId()));
        para.put(i++, new DbParameter(Types.BIGINT, curioLotteryNoticeData.getTime()));
        if (getDb().executeNoneQuery(insertCurioLotteryNoticeDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected CurioLotteryNoticeData parseData(ResultSet rs) throws SQLException {
        CurioLotteryNoticeData curioLotteryNoticeData = new CurioLotteryNoticeData();
        curioLotteryNoticeData.setServerId(rs.getLong("serverId"));
        curioLotteryNoticeData.setUserId(rs.getLong("userId"));
        curioLotteryNoticeData.setType(rs.getInt("type"));
        curioLotteryNoticeData.setRewardId(rs.getString("rewardId"));
        curioLotteryNoticeData.setTime(rs.getLong("time"));

        return curioLotteryNoticeData;
    }
}
